var menuDepressed;
var menuDown;
var activeButton = null;
var doPrompt;
doPrompt=true;

menuDepressed=null;
menuDown=false;

function browserDetect(){
	var m_Name = navigator.appName;
	var m_Ver = navigator.appVersion;
	var m_MinorVer = navigator.appMinorVersion;
	alert(m_Ver);
	var m_UserMsg;
	if (m_Ver.indexOf ("MSIE 8.0") != -1 || m_Ver.indexOf ("MSIE 6.0") != -1 || m_Ver.indexOf ("MSIE 7.0") != -1 ){
		m_UserMsg = "";
	} else {
		m_UserMsg = "FLSACWIS requires you to have Microsoft <u>Internet Explorer version 5.5 and higher</u>." +
		"You have: <i>" + m_Name + "</i> " + m_Ver + "." ;
	}

	if (m_UserMsg != "" ) {
		m_UserMsg =
		"<span style='color: red;' class='SectionSubTitle'><b>WARNING: <u>Your browser is not FLSACWIS compatible.</u></b></span><br/><br/>" +
		m_UserMsg ;
		showPrompt (m_UserMsg, "Close");
	}
}

function showMessage() {
	var m_URL = "common/ModalPrompt.html";
	var m_args = new Object();
	m_args.title = "Access denied. The role associated with your ID does not have authorization to execute this link. \n Please contact your system administrator with any questions.";
	m_args.button = "OK";
	window.showModalDialog(m_URL, m_args, "dialogHeight:10;status:no;help:no");
}

var prevLeft = "";
function initPageSpecific(){
	// Somehow expression doesnt seem to work hence I am using a temporary variable!!!
	prevLeft = document.getElementById("startup").style.left;
	browserDetect();
}

function expandOutliner(){
	document.getElementById("left").width = "100%" ;
	document.getElementById("right").width = "1%";
	document.getElementById("righttoolbar").style.display = "none" ;
	document.getElementById("outliner").style.borderRightColor = 'white';
	document.getElementById("startup").style.display = "none" ;
}

function expandStartup(){
	document.getElementById("left").width = "1%";
	document.getElementById("right").width = "100%" ;
	document.getElementById("lefttoolbar").style.display = "none" ;
	document.getElementById("startup").style.borderRightColor = 'white';
	document.getElementById("outliner").style.display = "none" ;
//	document.getElementById("startup").style = "left:10px";
}

function resetPage(){
	document.getElementById("left").width =  "50%" ;
	document.getElementById("right").width = "50%";
//	document.getElementById("righttoolbar").style.display = "inline" ;
	document.getElementById("lefttoolbar").style.display = "inline" ;
	document.getElementById("startup").style.display = "inline" ;
	document.getElementById("outliner").style.display = "inline" ;
	document.getElementById("outliner").style.borderRightColor = 'silver';
	document.getElementById("startup").style.borderRightColor = 'silver';
//	document.getElementById("startup").style = "left:expression( (document.body.clientWidth /2) + 10);";
	}

function Browser() {
  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

}

//----------------------------------------------------------------------------
// Code for handling the menu bar and active button.
//----------------------------------------------------------------------------
function resetMenu(){
	if (menuDepressed != null){
		activeButton = null;
		resetButton(menuDepressed);
	}
}

function acceleratorKey(){
//alert(event.keyCode);

	if (event.ctrlKey && event.keyCode == 37) {
		m_src = document.getElementById("expandLeft");

		if (m_src.src.search("left") && m_src.parentElement.style.display != "none" ){
			m_src.fireEvent("onclick");
		} else {
			document.getElementById("expandRight").fireEvent("onclick");
		}
		 return true;
	}

	if (event.ctrlKey && event.keyCode == 39) {
		m_src = document.getElementById("expandRight");

		if (m_src.src.search("right") && m_src.parentElement.style.display != "none"){
			m_src.fireEvent("onclick");
		} else {
			document.getElementById("expandLeft").fireEvent("onclick");
		}
		 return true;
	}

	if (event.keyCode==27){
		resetMenu();
		return true;
	}

	// Check for accelerators and execute them
	if (event.altKey && event.keyCode==82){
		 button = document.getElementById("create").firstChild;
		 evalButton(button,"createMenu");
		return true;
	}

	if (event.altKey && event.keyCode==77){
		 button = document.getElementById("maintain").firstChild;
		 evalButton(button,"maintainMenu");
		 return;
	}

	if (event.altKey && event.keyCode==83){
		 button = document.getElementById("search").firstChild;
		 evalButton(button,"searchMenu");
		return true;
	}

	if (event.altKey && event.keyCode==85){
		 button = document.getElementById("utilities").firstChild;
		 evalButton(button,"utilitiesMenu");
		return true;
	}

	if (event.altKey && event.keyCode==79){
		 button = document.getElementById("adoption").firstChild;
		 evalButton(button,"adoptionMenu");
		return true;
	}

	// Check for accelerators and execute them
	if (event.altKey && event.keyCode==76){
		 button = document.getElementById("help").firstChild;
		 evalButton(button,"helpMenu");
		return true;
	}

	return false;
}



function evalButton(button,menuId){
  // Blur focus from the link to remove that annoying outline.

  button.blur();

  // Associate the named menu to this button if not already done.
  // Additionally, initialize menu display.

  if (button.menu == null) {
    button.menu = document.getElementById(menuId);
    if (button.menu.initialized == null)
      menuInit(button.menu);
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.

  if (button.onmouseout == null){
    button.onmouseout = buttonOrMenuMouseout;
    button.loosefocus = buttonOrMenuMouseout;
	}

  // Exit if this button is the currently active one.

  if (button == activeButton)
    return false;

  // [END MODIFIED]

  // Reset the currently active button, if any.

  if (activeButton != null)
    resetButton(activeButton);

  // Activate this button, unless it was the currently active one.

  if (button != activeButton) {
    depressButton(button);
    activeButton = button;
  }
  else
    activeButton = null;

}

function buttonClick(event, menuId) {

  var button;

  // Get the target button element.
   if (window.event.srcElement.id == ""){
   		button = window.event.srcElement;
   } else {
	    button = window.event.srcElement.parentElement;
   }

  return evalButton(button,menuId);
}

function buttonMouseover(event, menuId) {

  var button;

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Activates this button's menu if no other is currently active.

  if (activeButton == null) {
    buttonClick(event, menuId);
    return;
  }

  // [END MODIFIED]

  // Find the target button element.

    button = window.event.srcElement;

  // If any other button menu is active, make this one active instead.

  if (activeButton != null && activeButton != button)
    buttonClick(event, menuId);
}

function depressButton(button) {
  menuDown=true;
  menuDepressed=button;

  var x, y;

  // Update the button's style class to make it look like it's
  // depressed.

  button.className += " menuButtonActive";

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the button, if not already done.

  if (button.onmouseout == null)
    button.onmouseout = buttonOrMenuMouseout;
  if (button.menu.onmouseout == null)
    button.menu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Position the associated drop down menu under the button and
  // show it.

  x = getPageOffsetLeft(button);
  y = getPageOffsetTop(button) + button.offsetHeight;

  // For IE, adjust position.

   x += button.offsetParent.clientLeft;
   y += button.offsetParent.clientTop;

   y -= 2;

  button.menu.style.left = x + "px";
  button.menu.style.top  = y + "px";
  button.menu.style.visibility = "visible";
	try{
		button.menu.getElementsByTagName("div")[0].focus();
	} catch (e) {
		// Do Nothing
		;
	}
}

function resetButton(button) {
  menuDown=false;
  menuDepressed=null;

  // Restore the button's style class.

  removeClassName(button, "menuButtonActive");

  // Hide the button's menu, first closing any sub menus.

  if (button.menu != null) {
    closeSubMenu(button.menu);
    button.menu.style.visibility = "hidden";
  }
}

//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function menuMouseover(event) {

  var menu;
  // Find the target menu element.

    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  // Close any active sub menu.
  if (menu.activeItem != null)
    closeSubMenu(menu);
}

function menuItemMouseover(event, menuId) {

  var item, menu, x, y;

  // Find the target item element and its parent menu element.

  item = getContainerWith(window.event.srcElement, "SPAN", "menuItem");
  menu = getContainerWith(item, "DIV", "menu");

  // Close any active sub menu and mark this one as active.

  if (menu.activeItem != null)
    closeSubMenu(menu);
  menu.activeItem = item;

  // Highlight the item element.

  item.className += " menuItemHighlight";

  // Initialize the sub menu, if not already done.

  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
    if (item.subMenu.initialized == null)
      menuInit(item.subMenu);
  }

  // [MODIFIED] Added for activate/deactivate on mouseover.

  // Set mouseout event handler for the sub menu, if not already done.

  if (item.subMenu.onmouseout == null)
    item.subMenu.onmouseout = buttonOrMenuMouseout;

  // [END MODIFIED]

  // Get position for submenu based on the menu item.
  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);

  // Adjust position to fit in view.

  var maxX, maxY;

  maxX = document.body.scrollLeft + document.body.clientWidth;
  maxY = document.body.scrollTop  + document.body.clientHeight;

  maxX -= item.subMenu.offsetWidth;
  maxY -= item.subMenu.offsetHeight;

  if (x > maxX)
    x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
      + (menu.offsetWidth - item.offsetWidth)) ;
  y = Math.max(0, Math.min(y, maxY));

	x -= 12;

  // Position and show the sub menu.

  item.subMenu.style.left = x + "px";
  item.subMenu.style.top  = y + "px";
  item.subMenu.style.visibility = "visible";

	try{
		item.subMenu.getElementsByTagName("div")[0].focus();
	} catch (e) {
		// Do Nothing
		;
	}

  // Stop the event from bubbling.

  window.event.cancelBubble = true;
}

function closeSubMenu(menu) {

  if (menu == null || menu.activeItem == null)
    return;

  // Recursively close any sub menus.

  if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
    menu.activeItem.subMenu.style.visibility = "hidden";
    menu.activeItem.subMenu = null;
  }
  removeClassName(menu.activeItem, "menuItemHighlight");
  menu.activeItem = null;
}

function menuItemMousehover(p_element){
	p_element.className += " menuItemHighlight";
	p_element.cursor='hand';
}

function menuItemMousehoverout(p_element){
	removeClassName(p_element, "menuItemHighlight");
}

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function buttonOrMenuMouseout(event) {

  var el;

  // If there is no active button, exit.

  if (activeButton == null)
    return;

  // Find the element the mouse is moving to.

  el = window.event.toElement;

  // If the element is not part of a menu, reset the active button.

  if (getContainerWith(el, "DIV", "menu") == null) {
    resetButton(activeButton);
    activeButton = null;
  }

}

// [END MODIFIED]

//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function menuInit(menu) {

  var itemList, spanList
  var textEl, arrowEl;
  var itemWidth;
  var w, dw;
  var i, j;

  // For IE, replace arrow characters.

    menu.style.lineHeight = "2.5ex";
    spanList = menu.getElementsByTagName("SPAN");
    for (i = 0; i < spanList.length; i++)
      if (hasClassName(spanList[i], "menuItemArrow")) {
        spanList[i].style.fontFamily = "Webdings";
        spanList[i].firstChild.nodeValue = "4";
      }

  // Find the width of a menu item.

  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0)
    itemWidth = itemList[0].offsetWidth;
  else
    return;

  // For items with arrows, add padding to item text to make the
  // arrows flush right.

  for (i = 0; i < itemList.length; i++) {
    spanList = itemList[i].getElementsByTagName("SPAN")
    textEl  = null
    arrowEl = null;
    for (j = 0; j < spanList.length; j++) {
      if (hasClassName(spanList[j], "menuItemText"))
        textEl = spanList[j];
      if (hasClassName(spanList[j], "menuItemArrow"))
        arrowEl = spanList[j];
    }
    if (textEl != null && arrowEl != null)
      textEl.style.paddingRight = (itemWidth
        - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
  }

  // Fix IE hover problem by setting an explicit width on first item of
  // the menu.

    w = itemList[0].offsetWidth;
    itemList[0].style.width = w + "px";
    dw = itemList[0].offsetWidth - w;
    w -= dw;
    itemList[0].style.width = w + "px";

  // Mark menu as initialized.

  menu.initialized = true;
}

//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith(node, tagName, className) {

  // Starting with the given node, find the nearest containing element
  // with the specified tag name and style class.

  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
        hasClassName(node, className))
      return node;
    node = node.parentNode;
  }

  return node;
}

function hasClassName(el, name) {

  var i, list;

  // Return true if the given element currently has the given class
  // name.

  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function removeClassName(el, name) {

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the x coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function MenuItemClick(p_url){
	if (p_url == '' | p_url == null ) {
		p_url=contextRoot + "common/WorkInProgress.jsp";
	} else {
		p_url = p_url + '&fromWhere=desktop';
	}
	try {
		// Try to use Master.js function to open window.
		launchWindow(p_url);
		 resetMenu();
	} catch (e) {
		// For some reason the function is not accessible, launch window anyways
		window.open(p_url,"_blank","left=0,top=0,titlebar=no,menubar=no,scrollbars=yes,status=yes,resizable=yes");
	}
}


function about(){
	resetMenu();
	window.showModalDialog(contextRoot + "common/About.jsp" , "" , "dialogWidth:450px;dialogHeight:275px;status:no;help:no;scroll:no");
}

function leftImageclicked(){
	var m_src =  event.srcElement;

	if (m_src.src == undefined || m_src.src == null)
		return;

	if (m_src.src.search("right") > 0){
		m_src.src = contextRoot + "images/blue_left_arrow.gif";
		m_src.title = "Undo Expand";
		expandOutliner();
	} else {
		m_src.src = contextRoot + "images/blue_right_arrow.gif";
		m_src.title = "Expand Right";
		resetPage();
	}
}


function rightImageclicked(){
	var m_src =  event.srcElement;

	if (m_src.src == undefined || m_src.src == null)
		return;

	if (m_src.src.search("left") > 0){
		m_src.src = contextRoot + "images/blue_right_arrow.gif";
		m_src.title = "Undo Expand";
		expandStartup();
	} else {
		m_src.src = contextRoot + "images/blue_left_arrow.gif";
		m_src.title = "Expand Left";
		resetPage();
	}
}

function logout() {

	if (!isAnyWindowOpen())
		if (showPrompt("Are you sure you want to logout?","YesNoCancel") == "YES"){
			cleanTempDocumentDir();
			logoutComplete();
		}
}

function logoutComplete(){
	top.document.getElementById("bdy").onbeforeunload = "";
	top.navigate(contextRoot + "ICHAINLOGON.do?action=LOGOUT");
}

function createFinancial() {

	launchWindow(contextRoot + "SM04B_CreateFinancialWork.do?action=CREATE&fromWhere=desktop","_blank","left=0,top=0,titlebar=no,menubar=no,scrollbars=yes,status=yes,resizable=yes");

}

function createCase() {

	launchWindow(contextRoot + "SM04B_CreateCaseWork.do?action=CREATE&fromWhere=desktop","_blank","left=0,top=0,titlebar=no,menubar=no,scrollbars=yes,status=yes,resizable=yes");

}

function createProvider() {

	launchWindow(contextRoot + "SM04B_CreateProviderWork.do?action=CREATE&fromWhere=desktop","_blank","left=0,top=0,titlebar=no,menubar=no,scrollbars=yes,status=yes,resizable=yes");

}

function performSearch() {

	launchWindow(contextRoot + "CM10_UtilitySearch.do?action=OPEN&fromWhere=desktop","_blank","left=0,top=0,titlebar=no,menubar=no,scrollbars=yes,status=yes,resizable=yes");

}

function printDesktop() {
	var parm = new Object();
	parm.outliner = document.getElementById("outliner");
	parm.messages = document.getElementById("startup");

	window.showModalDialog( contextRoot + "common/DesktopPrint.jsp",  parm ,"dialogWidth:340px;dialogHeight:10px;status:no;help:no;scroll:no");
}

function refreshMessages(){

	if (showPrompt ("<b>Are you sure you want to refresh the Desktop?</b><br/><br/>This will refresh the <u>entire</u> desktop." , "YesNoCancel") == "YES"){
		doPrompt = false;
		window.navigate(contextRoot + "LOGON.do?action=REFRESH");

	}
}

function warnUser(){
	if (doPrompt == true){
		cleanTempDocumentDir();
		event.returnValue = "************************************************************************" +
							"\n                                 WARNING\n" +
							"************************************************************************" +
							"\n\nYou are about to leave the FLSACWIS application." +
							"\nPlease save all associated work." +
							"\n\n" +
							"************************************************************************";
	}

}


function menuLoseFocus(p_event, isBegin){
	try{
		if(p_event.keyCode == 9) {
			if((isBegin && p_event.shiftKey) || ((!isBegin) && (!p_event.shiftKey)))
				resetMenu();
		}
	} catch(e){
		;
	}
}